import { createStore, Store, useStore as useVuexStore } from 'vuex'
import type { IRootState, IStoreType } from './types'
import login from './login/login'
import main from './main/main'
import home from './home/home'
import songListInfo from './songListInfo/songListInfo'
import music from './music/music'
import localCache from '@/utils/cache'
const store = createStore<IRootState>({
  state() {
    return {}
  },
  mutations: {},
  getters: {},
  actions: {},
  modules: {
    login,
    main,
    home,
    songListInfo,
    music
  }
})

export function setupStore() {
  const token = localCache.getCache('token')
  if (token) {
    localCache.deleteCache('token')
    const password = localCache.getCache('password')
    const userName = localCache.getCache('userName')
    const userFrom = {
      userPassword: password,
      userName: userName
      // store.dispatch('login/loadLocalLogin')
      // store.dispatch('getInitialDataAction')
    }
    store.dispatch('login/loginAction', { ...userFrom })
    store.dispatch('main/getPlayListAction')
    store.dispatch('main/getSingListAction')
    store.dispatch('main/getNewSingAction')
  }
}

export function useStore(): Store<IStoreType> {
  return useVuexStore()
}

export default store
